package com.dk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dk.pojo.TRole;
import com.dk.pojo.User;
import com.dk.service.RoleService;
import com.dk.service.UserService;
import com.dk.controller.MD5Utils;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("userController")
public class UserController {

	//ע��UserService
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	private static Logger log = Logger.getLogger(UserController.class);
	
	//��תע��ҳ
	@RequestMapping("toRegister")
	public String toRegister() {
		return "user/register";
	}
	
	//ע���߼�
	@RequestMapping("registerInfo")
	@ResponseBody
	public String registerInfo(User user, String username) throws Exception {
		User u = null;
		try {
			u = userService.registerInfo(username);
			if (u != null) {
				return "1";//˵�����˻��ѱ�ע��
			}
			String password = MD5Utils.getEncryptedPwd(user.getUpassword());
			user.setUpassword(password);
			user.setErrorcount(0);
			userService.insertUser(user);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return "2";//˵������ע��ɹ�
	}
	
	//��¼�߼�
	@RequestMapping("toLogin")
	@ResponseBody
	public String toLogin(String username, String upassword, String vcode, HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		String code = (String) session.getAttribute("code");
		User u = null;
		try {
			if (code == null || "".equals(code) || !code.equalsIgnoreCase(vcode)) {
				return "5";//��֤�����
			}
			u = userService.registerInfo(username);
			if (u == null) {
				return "1";//˵���û�������
			}else if(u.getErrorcount() >= 3) {
				return "2";//˵���˻��ѱ�����
			}else if (u.getUpassword() == null || !MD5Utils.validPassword(upassword, u.getUpassword())) {
				u.setErrorcount(u.getErrorcount() + 1);
				userService.updateByUser(u);
				return "4";//˵���������
			}
			if (u.getErrorcount() > 0) {
				u.setErrorcount(0);
				userService.updateByUser(u);
			}
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		session.setAttribute("u", u);
		return "3";//��½�ɹ�
	}
	
	//��תչʾҳ��
	@RequestMapping("toTreeView")
	public String toTreeView() {
		return "user/treeview";
	}
	
	//��ת�û���ѯ
	@RequestMapping("toList")
	public String toList() {
		return "user/list";
	}
	
	//��ѯ�û���Ϣ
	@RequestMapping("getServerUserInfo")
	@ResponseBody
	public Map<String, Object> getServerUserInfo(String username, @RequestParam(defaultValue="1") Integer pageNum, @RequestParam(defaultValue="10") Integer pageSize) {
		PageInfo<User> page = null;
		Map<String, Object> map = null;
		try {
			page = userService.getServerUserInfo(username, pageNum, pageSize);
			map = new HashMap<>();
			map.put("total", page.getTotal());
			map.put("rows", page.getList());
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return map;
	}
	
	//��ת���û�����ҳ��
	@RequestMapping("toAdd")
	public String toAdd(Model model) {
		//����ɫ��ѯ����
		List<TRole> roleList = roleService.findRoleList();
		model.addAttribute("roles", roleList);
		return "user/add";
	}
	
	//����
	@RequestMapping("addUser")
	@ResponseBody
	public String addUser(User user,@RequestParam("roleIds[]") Integer[] roleIds) {
		userService.addUser(user,roleIds);
		return "1";
	}
	
	//����ɾ��
	@RequestMapping("deleteInfoById/{uid}")
	@ResponseBody
	public String deleteInfoById(@PathVariable("uid") Integer uid) {
		userService.deleteInfoById(uid);
		return "1";
	}
	
	//��ת�޸�ҳ��
	@RequestMapping("toUpdate/{uid}")
	public String toUpdate(Model model, @PathVariable("uid") Integer uid) {
		//����id���û���ѯ����
		User user = userService.findUserByUid(uid);
		//����uid��ѯ�û���ӵ�еĽ�ɫ
		List<Integer> roleIds = roleService.findRoleByUid(uid);
		//�����н�ɫ��ѯ����
		List<TRole> roleList = roleService.findRoleList();
		model.addAttribute("user", user);
		model.addAttribute("roleIds", roleIds);
		model.addAttribute("roles", roleList);
		
		return "user/update";
	}
	
	//�޸�
	@RequestMapping("updateUser")
	@ResponseBody
	public String updateUser(User user,@RequestParam("roleIds[]") Integer[] roleIds) {
		userService.updateUser(user,roleIds);
		return "1";
	}
	
	//�˳���¼
	@RequestMapping("toLoginOut")
	public String toLoginOut(HttpServletRequest request) {
		HttpSession session = null;
		try {
			session = request.getSession();
			session.removeAttribute("u");
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return "redirect:/";
	}
}
